package oa.hleast.xswl.controller.admin.pms;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.xswl.pojo.domain.pms.PmsPointThing;
import oa.hleast.xswl.service.pms.PmsPointThingService;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;


@Api(tags = "【系统管理】积分物品表")
@RestController("AdminPmsPointThingController")
@RequestMapping("/api.admin/v1/pmsPointThings")
@Slf4j
@AllArgsConstructor
public class PmsPointThingController {

    private PmsPointThingService pmsPointThingService;

    @ApiOperation(value = "列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping
    public Result list(
            String queryMode,
            Integer page,
            Integer limit,
            String name) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                LambdaQueryWrapper<PmsPointThing> queryWrapper = new LambdaQueryWrapper<PmsPointThing>()
                        .like(StrUtil.isNotBlank(name), PmsPointThing::getName, name)
                        .orderByAsc(PmsPointThing::getSort)
                        .orderByDesc(PmsPointThing::getGmtModified)
                        .orderByDesc(PmsPointThing::getGmtCreate);

                Page<PmsPointThing> result = pmsPointThingService.page(new Page<>(page, limit), queryWrapper);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }


    @ApiOperation(value = "新增", httpMethod = "POST")
    @PostMapping
    public Result add(@RequestBody PmsPointThing body) {
        boolean status = pmsPointThingService.save(body);
        return Result.judge(status);
    }

    @ApiOperation(value = "详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    @GetMapping("/{id}")
    public Result detail(@PathVariable Long id) {
        return Result.success(pmsPointThingService.getById(id));
    }

    @ApiOperation(value = "修改", httpMethod = "POST")
    @PostMapping("/put")
    public Result update(
            @RequestBody PmsPointThing body) {
        body.setGmtModified(new Date());
        boolean status = pmsPointThingService.updateById(body);
        return Result.judge(status);
    }

    @ApiOperation(value = "删除标签", httpMethod = "POST")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class)
    @PostMapping("/delete/{ids}")
    public Result delete(@PathVariable("ids") String ids) {
        boolean status = pmsPointThingService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }
}
